//
//  XYCallingTopToolBarView.m
//  xylink_app
//
//  Created by gao chenyang on 2020/6/20.
//  Copyright © 2020 ainemo. All rights reserved.
//

#import "XYCallingTopToolBarView.h"
@interface XYCallingTopToolBarView(){
    int interval;
}
//会议室号
@property (weak, nonatomic) IBOutlet UILabel * meetingRoomNumLab;
//通话时间
@property (weak, nonatomic) IBOutlet UILabel * inTalkingIntervalLab;
//网络状态
@property (weak, nonatomic) IBOutlet UIImageView * networkStatusImageView;
//挂断
@property (nonatomic, weak) IBOutlet UIButton * hangupBtn;

@property (nonatomic, strong) NSTimer * inTalkingIntervalTimer;
@end

@implementation XYCallingTopToolBarView
+(instancetype)createView{
    XYCallingTopToolBarView * view = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil].firstObject;
    [view _setupDefault];
    [view _setupSubViews];
    return view;
}

#pragma mark- setup

-(void)_setupDefault{
    interval = 0;
}

-(void)_setupSubViews{
    [self.hangupBtn setTitle:@"挂断" forState:UIControlStateNormal];
    self.hangupBtn.enabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.hangupBtn.enabled = YES;
    });
}

#pragma mark- actions

- (IBAction)clickHangupEvent:(id)sender {
    if (self.clickHangupBlock) {
        self.clickHangupBlock();
    }
}

- (IBAction)tapNetworkStatusEvent:(id)sender {
    if (self.tapNetworkStatusBlock) {
        self.tapNetworkStatusBlock();
    }
}

#pragma mark- public method
-(void)updateMeetingRoomNum:(NSString *)roomNum{
    if (roomNum && roomNum.length > 0) {
        self.meetingRoomNumLab.text = roomNum;
    }
}

- (void)updateOnNetworkIndicatorLevel:(int)level{
    NSString *imageName = @"network_";
    imageName = [NSString stringWithFormat:@"%@%d",imageName,level];
    self.networkStatusImageView.image = [UIImage imageNamed:imageName];
}

-(void)startMeetingTimer{
    if (!self.inTalkingIntervalTimer) {
        self.inTalkingIntervalTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(changeInTalkingInterval) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.inTalkingIntervalTimer forMode:NSDefaultRunLoopMode];
    }
}

-(void)stopMeetingTimer{
    [self.inTalkingIntervalTimer invalidate];
    self.inTalkingIntervalTimer = nil;
}

- (void)changeInTalkingInterval{
    interval++;
    int second = interval % 60;
    int minute = (interval % 3600) / 60;
    int hour = interval / 3600;
    self.inTalkingIntervalLab.text = [NSString stringWithFormat:@"%02d:%02d:%02d",hour,minute,second];
}
@end
